/**
 * Created by cqmcqm on 2015/6/1.
 */


var FESW=FESW||function(){};

/**
 * 选择器
 */
FESW.prototype.$=function(selector)
{
    return document.querySelector(selector);
}

/**
 * 设置CSS
 */
FESW.prototype.setCss=function(domObj,cssObj)
{
    for(i in cssObj){
        domObj.style[i]=cssObj[i];
    }
}

/**
 * 创建DOM,多用于创建悬浮框
 */
FESW.prototype.createDom=function(tag,positionType,x,y,width,height,centerFlg,extraCss,classname)
{
    var dom=document.createElement(tag);
    classname&&dom.classList.add(classname);
    this.setCss(dom,{
        position:positionType,
        left:x+'px',
        top:y+'px',
        width:width+'px',
        height:height+'px'
    });
    if(centerFlg){
        this.setCss(dom,{
            marginLeft:-width/2+'px',
            marginTop:-height/2+'px'
        });
    }
    this.setCss(dom,extraCss);
    return dom;
}

/**
 * 用1600个css3动画进行性能测试
 */
FESW.prototype.performanceTest=function()
{
    var time=new Date().getTime();
    var max=0;
    var flg=true;
    var eleNum=1600;
    !function(num){
        for(var i=0;i<num;i++){
            var elementDom=this.createDom('div','absolute',parseInt(i/Math.ceil(Math.sqrt(num)))*12,i%Math.ceil(Math.sqrt(num))*12,10,10,false,
                {background:"#"+this.fillColorNum(0xffffff*i/num),transition:'all 2s','-webkit-transition':'all 2s'});
            document.body.appendChild(elementDom);
        }
    }(eleNum);

    var timeIntervalDom=this.createDom('div','absolute',600,200,100,100,false,{background:"#000",color:"#fff"});
    timeIntervalDom.innerText=new Date().getTime();
    document.body.appendChild(timeIntervalDom);

    !function updateText(){
            max=Math.max(dom.innerText=new Date().getTime()-time,max);
            time=new Date().getTime();
            requestAnimationFrame(updateText);
    }();

    setInterval(function toggle(){
        max=0;
        var div=document.querySelectorAll('div');
        for(var i=0;i<div.length;i++){
            this.setCss(div[index],{marginLeft:(flg?600:0)+'px'});
        }
        flg=!flg;
        setTimeout(function(){
            alert(max);
        },3000);
    },5000);
}

/**
 * 补全颜色数字
 */
FESW.prototype.fillColorNum=function(num){
    var res=Number(num.toFixed(0)).toString(16);
    var stuff='';
    for(var i=0;i<(6-res.length);i++){
        stuff+='0';
    }
    return stuff+res;
}
